package controllers

import (
	"demo/logic"
	"demo/models"
	"demo/utils"
	"fmt"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
)

type GoodController struct {
	beego.Controller
}

func (c *GoodController) Form() {

	o := orm.NewOrm()
	var t []models.GoodsType
	o.QueryTable("goods_type").All(&t)
	c.Data["Data"] = t
	c.TplName = "form.html"
}

func (c *GoodController) InsertGood() {

	name := c.GetString("name")
	price := c.GetString("price")
	typeId := c.GetString("type")
	status := c.GetString("status")
	f, h, _ := c.GetFile("images")
	if h.Size > 2*1024*1024 {
		c.Data["json"] = map[string]interface{}{
			"code": 202,
			"msg":  "上传的文件只允许2M之内",
		}
		c.ServeJSON()
	}
	defer f.Close()
	desc := c.GetString("desc")
	d, _ := utils.Upload(h)
	fmt.Println(desc)
	valid := validation.Validation{}
	valid.Required(name, "name").Message("商品名称不能为空")
	valid.Required(price, "price").Message("商品单价不能为空")
	valid.Required(typeId, "typeId").Message("商品类型不能为空")
	valid.Required(status, "status").Message("商品状态不为空")
	if valid.HasErrors() {
		for _, v := range valid.Errors {
			c.Data["json"] = map[string]interface{}{
				"code": 202,
				"msg":  v.Message,
			}
			c.ServeJSON()
		}
	}
	p, _ := strconv.Atoi(price)
	t, _ := strconv.Atoi(typeId)
	o := orm.NewOrm()
	g := models.Good{
		Name:   name,
		Price:  float64(p),
		TypeId: int64(t),
		Status: status,
		Img:    d,
		Desc:   desc,
	}
	_, err := o.Insert(&g)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 200,
			"msg":  err.Error(),
		}
		c.ServeJSON()
	}
	c.Redirect("/list", 302)
}

type GoodList struct {
	NowPage  int
	ShowPage int
	Pages    int
	Data     []models.Good
	Name     string
	Fy       []FY
}

type FY struct {
	Ym   int
	Name string
}

func (c *GoodController) ListGood() {
	page := c.GetString("page")
	name := c.GetString("name")

	logs.Info(page)
	logs.Info(name)

	p, _ := strconv.Atoi(page)
	showPage, pages, g := logic.List(p, name)

	//分页
	var f []FY
	for i := 1; i <= int(pages); i++ {
		f = append(f, FY{
			Ym:   i,
			Name: name,
		})
	}
	//上一页
	var upPage, nextPage int
	if p-1 > 0 {
		upPage = p - 1
	} else {
		upPage = 1
	}
	//下一页
	if p+1 < int(pages) {
		nextPage = p + 1
	} else {
		nextPage = int(pages)
	}
	GL := GoodList{
		NowPage:  p,
		ShowPage: showPage,
		Pages:    int(pages),
		Data:     g,
		Name:     name,
		Fy:       f,
	}
	fmt.Println(GL)
	//JsGl, _ := json.Marshal(GL)
	//fmt.Println(JsGl)
	c.Data["upPage"] = upPage
	c.Data["nextPage"] = nextPage
	c.Data["NowPage"] = GL.NowPage
	c.Data["ShowPage"] = GL.ShowPage
	c.Data["Pages"] = GL.Pages
	c.Data["Data"] = GL.Data
	c.Data["Name"] = GL.Name
	c.Data["Fy"] = GL.Fy
	c.TplName = "list.html"
}

func (c *GoodController) DeleteGood() {
	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"message": "这里是删除，待开发",
	}
	c.ServeJSON()
}

func (c *GoodController) UpdateGood() {
	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"message": "这里是修改，待开发",
	}
	c.ServeJSON()
}
